package vn.fpt.snake.commons;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.List;
import vn.fpt.snake.dataAccess.DataAccess;
import vn.fpt.snake.entity.Settings;

public class Utility {
	DataAccess dataAccess;
	Settings settings;
	int width;
	int height;
	int dotSize;
	public Utility(){
		dataAccess = new DataAccess();
		settings = dataAccess.getSettings();
		width = settings.getWidth();
		height = settings.getHeight();
		dotSize = settings.getDotSize();
	}
	public Point randomPoint(List<Point> points){
		if(points != null){
			Point randomPoint = null;
			boolean flag = false;
			while(!flag){
				randomPoint = randomPoint();
				flag = true;
				for(Point point : points){
					if(randomPoint.x == point.x && randomPoint.y == point.y){
						flag = false;
						break;
					}
				}
			}
			return randomPoint;
		}
		return null;
	}
	
	public Point randomPoint(){
		int XUnit = (width / dotSize) - 2;
		int YUnit = (height / dotSize) - 2;
		int randomXUnit = (int) (XUnit * Math.random());
		int randomYUnit = (int) (YUnit * Math.random());
		int randomX = randomXUnit * dotSize;
		int randomY = randomYUnit * dotSize;
		return new Point(randomX, randomY);
	}
	
	public BufferedImage rotateImage(BufferedImage image1, int numberRotate) {
        if (image1 != null && numberRotate > 0) {
        	BufferedImage image = image1;
        	int residualNumber = numberRotate % 3;
        	numberRotate = residualNumber == 0 ? 3 : residualNumber;
        	int count = 0;
        	while(count < numberRotate){
	            BufferedImage tmp = new BufferedImage(image.getHeight(),
	                    image.getWidth(), BufferedImage.TYPE_INT_ARGB);
	            for (int i1 = 0; i1 < tmp.getHeight(); i1++) {
	                for (int j1 = 0; j1 < tmp.getWidth(); j1++) {
	                    tmp.setRGB(j1, i1, image.getRGB(j1, i1));
	                }
	            }
	            int x2 = 0;
	            int dotSize = settings.getDotSize();
	            int y2 = dotSize - 1;
	            for (int x1 = 0; x1 < dotSize; x1++) {
	                for (int y1 = 0; y1 < dotSize; y1++) {
	                    image.setRGB(x1, y1, tmp.getRGB(x2, y2));
	                    x2++;
	                }
	                y2--;
	                x2 = 0;
	            }
	            count++;
        	}
        	return image;
        }
        return null;
    }
	
	public BufferedImage rotateImage(BufferedImage img) {
        if (img != null) {
        	BufferedImage image = img;
            BufferedImage tmp = new BufferedImage(image.getHeight(),
                    image.getWidth(), BufferedImage.TYPE_INT_ARGB);
            for (int i1 = 0; i1 < tmp.getHeight(); i1++) {
                for (int j1 = 0; j1 < tmp.getWidth(); j1++) {
                    tmp.setRGB(j1, i1, image.getRGB(j1, i1));
                }
            }
            int x2 = 0;
            int dotSize = settings.getDotSize();
            int y2 = dotSize - 1;
            for (int x1 = 0; x1 < dotSize; x1++) {
                for (int y1 = 0; y1 < dotSize; y1++) {
                    image.setRGB(x1, y1, tmp.getRGB(x2, y2));
                    x2++;
                }
                y2--;
                x2 = 0;
            }
            return image;
        }
        return null;
    }
}
